package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishServiceImpl setmealDishService;
    @Resource
    private AliOssUtil aliOssUtil;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Resource
    private DishMapper dishMapper;
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setImage(aliOssUtil.extractObjectKey(setmealDTO.getImage()));
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //批量插入套餐和菜品的关联数据
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageSize = setmealPageQueryDTO.getPageSize();
        int current = setmealPageQueryDTO.getPage();
        Page<Setmeal> page = new Page<>(current,pageSize);
        Page<SetmealVO> pageQuery = setmealMapper.pageSetmealWithCategory(page,setmealPageQueryDTO);
        pageQuery.getRecords().forEach(setmealVO -> {
            setmealVO.setImage(aliOssUtil.generatePresignedUrl(setmealVO.getImage()));
        });
        return new PageResult(pageQuery.getTotal(),pageQuery.getRecords());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //如果ids包含起售中的套餐，则抛出业务异常
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById( id);
            if(setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        this.removeByIds(ids);
        //删除setmealDish表中的套餐和菜品的关联数据 通过setmeal_Id 删除所有setmealDish表中的数据
        //构建查询条件
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);
    }

    @Override
    @Transactional
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setImage(aliOssUtil.generatePresignedUrl(setmeal.getImage()));
        List<SetmealDish> setmealDishs = setmealDishMapper.selectList(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setImage(aliOssUtil.extractObjectKey(setmealDTO.getImage()));
        setmealMapper.updateById(setmeal);
        setmealDishMapper.delete(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmealDTO.getId()));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //得先判断套餐是否可以起售 得看里面的菜品是否在售 如果里面的菜品有一个处于停售状态 则不能起售
        List<SetmealDish> setmealDishs = setmealDishMapper.selectList(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        for (SetmealDish setmealDish : setmealDishs) {
            Long dishId = setmealDish.getDishId();
            Integer dishStatus = dishMapper.selectById(dishId).getStatus();
            if(dishStatus == 0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateById(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        //根据分类id查询套餐
        //构造查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        //查询启售的套餐
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        return setmealMapper.selectList(wrapper);
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        //根据套餐id查询包含的菜品列表
        List<DishItemVO> dishItemVo = setmealMapper.getDishItemById(id);
        dishItemVo.forEach(dishItemVO -> {
            dishItemVO.setImage(aliOssUtil.generatePresignedUrl(dishItemVO.getImage()));
        });
        return dishItemVo;
    }
}
